package ro.gateway.aida.fnd;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: FundingTypeDB.java,v 1.1 2004/10/24 23:37:14 mihaipostelnicu Exp $
 */
public class FundingTypeDB extends DBPersistenceManager 
{
	private FundingTypeDB(PersistenceToken token) {
		super(token);
	}

	public static FundingTypeDB getManager(PersistenceToken token) {
		return new FundingTypeDB(token);
	}
      public FundingType getById(
            long id )
            throws SQLException
      {

            Connection con = getConnection();
            FundingType result = null;

            try
            {
                  PreparedStatement ps = con.prepareStatement( GET_BY_ID );
                  ps.setLong( 1, id );

                  ResultSet rs = ps.executeQuery();
                  while ( rs.next() )
                  {
                        if ( result == null )
                        {
                              result = new FundingType();
                              result.setId( id );
                              result.setHarm_code( rs.getString( 1 ) );
                        }
                        String lang = rs.getString( 2 );
                        result.setName( lang, rs.getString( 3 ) );
                  }
                  rs.close();

                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            return result;
      }

      public FundingType[] getAll()
            throws SQLException
      {

            Connection con = getConnection();
            FundingType[] result = null;
            ArrayList items = new ArrayList();

            try
            {
                  PreparedStatement ps = con.prepareStatement( GET_ALL );
                  ResultSet rs = ps.executeQuery();

                  FundingType lastItem = null;
                  while ( rs.next() )
                  {
                        long id = rs.getLong( 1 );
                        if ( ( lastItem != null ) && ( lastItem.getId() != id ) )
                        {
                              lastItem = null;
                        }

                        if ( lastItem != null )
                        {
                              String lang = rs.getString( 3 );
                              lastItem.setName( lang, rs.getString( 4 ) );
                        }
                        else
                        {
                              lastItem = new FundingType();
                              lastItem.setId( id );
                              lastItem.setHarm_code( rs.getString( 2 ) );
                              String lang = rs.getString( 3 );
                              lastItem.setName( lang, rs.getString( 4 ) );
                              items.add( lastItem );
                        }
                  }

                  rs.close();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            if ( items.size() > 0 )
            {
                  result = new FundingType[ items.size() ];
                  items.toArray( result );
            }

            return result;

      }

      public FundingType[] getAllByLang()
            throws SQLException
      {
            Connection con = getConnection();
            FundingType[] result = null;
            ArrayList items = new ArrayList();

            try
            {
                  PreparedStatement ps = con.prepareStatement( GET_ALL_BY_LANG );
                  ResultSet rs = ps.executeQuery();

                  while ( rs.next() )
                  {
                        FundingType item = new FundingType();

                        item.setId( rs.getLong( 1 ) );
                        item.setHarm_code( rs.getString( 2 ) );
                        String lang = rs.getString( 3 );
                        item.setName( lang, rs.getString( 4 ) );

                        items.add( item );
                  }

                  rs.close();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            if ( items.size() > 0 )
            {
                  result = new FundingType[ items.size() ];
                  items.toArray( result );
            }

            return result;

      }

      public static final String GET_BY_ID =
            "SELECT harm_code, lang, name " +
            "FROM funding_types " +
            "WHERE id = ?";

      public static final String GET_ALL =
            "SELECT id, harm_code, lang, name " +
            "FROM funding_types " +
            "ORDER BY id ASC";

      public static final String GET_ALL_BY_LANG =
            "SELECT id, harm_code, name " +
            "FROM funding_types " +
            "WHERE lang = ?";
}
